
def dedupe1(items):
    seen = set()
    for item in items:
        if item not in seen:
            yield item
            seen.add(item)

l = [1,2,3,2,4,66,19,2,4,66,5,43]
for i in dedupe1(l):
    print(i)

# 讲一个function 作为参数传入，可以将对象的某个字段或者属性去除重复项，更能适应复杂的数据结构
def dedupe(items, key=None):
    seen = set()
    for item in items:
        val = item if key is None else key(item)
        if val not in seen:
            # 将 item 作为生成器的 每一项
            yield item
            seen.add(val)
            print(seen)
        


d = [{'x':1,'y':2} , {'x':1,'y':4} , {'x':5,'y':6} ,{'x':1,'y':2} ]
# 过滤 x,y 都相同的元素
li = list(dedupe(d,lambda di: (di['x'],di['y'])))
print(li)
# 过滤 x 相同的元素
li = list(dedupe(d,lambda di: (di['x'])))
print(li)


